package fr.aprr.odyssee.client;


import org.json.Test;

import fr.aprr.odyssee.client.view.CanvasWidget;
import fr.aprr.odyssee.client.view.FlotChart;
import fr.aprr.odyssee.client.view.HeaderMenu;
import fr.aprr.odyssee.client.view.MainPanel;
import fr.aprr.odyssee.client.view.TablePanel;
import fr.aprr.odyssee.client.view.VehiculePanel;
import fr.aprr.odyssee.client.vue.VehiculeView;
import fr.aprr.odyssee.shared.FieldVerifier;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;


import com.google.gwt.user.client.ui.Widget;
import com.google.web.bindery.event.shared.EventBus;
import com.sencha.gxt.core.client.resources.ThemeStyles;
import com.sencha.gxt.core.client.util.Margins;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.Dialog;
import com.sencha.gxt.widget.core.client.TabItemConfig;
import com.sencha.gxt.widget.core.client.TabPanel;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.form.TextField;
import com.sencha.gxt.widget.core.client.menu.Menu;
import com.sencha.gxt.widget.core.client.menu.MenuBar;
import com.sencha.gxt.widget.core.client.menu.MenuBarItem;
import com.sencha.gxt.widget.core.client.menu.MenuItem;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Geolog implements EntryPoint {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting
	 * service.
	 */
	private final GreetingServiceAsync greetingService = GWT
			.create(GreetingService.class);

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		final TextButton sendButton = new TextButton("Search");
		final TextField nameField = new TextField();
		nameField.setText("Data");
		final Label errorLabel = new Label();

		// Text GXT
	//	TextButton testButton = new TextButton("Test GXT");
		Menu testMenu = new Menu();
		MenuItem item1 = new MenuItem("New");
		testMenu.add(item1);

		MenuItem item2 = new MenuItem("Open File");
		testMenu.add(item2);

		// We can add style names to widgets
		sendButton.addStyleName("sendButton");

		// Test with GXT
		MenuBar bar = new MenuBar();
		bar.addStyleName(ThemeStyles.get().style().borderBottom());
        bar.add(new MenuBarItem("File", testMenu));
        bar.add(new MenuBarItem("Configuation", testMenu));
        bar.add(new MenuBarItem("Affichage", testMenu));
        bar.add(new MenuBarItem("Archive", testMenu));
        
		RootPanel.get("headermenu").add(bar);
		 
		TabPanel mainPanel = new TabPanel();
		mainPanel.setAnimScroll(true);
		mainPanel.setTabScroll(true);
		mainPanel.setCloseContextMenu(true);
		
		// Add button send to server
		VerticalLayoutContainer secondLayout = new VerticalLayoutContainer();
		secondLayout.add(nameField);
		secondLayout.add(sendButton);
		secondLayout.add(errorLabel);
		
		MainPanel mainTable = new MainPanel();
		
		mainPanel.add(mainTable, "Dijon");
		mainPanel.add(secondLayout, "Paris");
		RootPanel.get("mainpanel").add(mainPanel);
		

		//Test MVP in second Panel

		// Create the popup dialog box
		final Dialog dialogBox = new Dialog();
		dialogBox.setBodyStyleName("pad-text");
		dialogBox.setHideOnButtonClick(true);
		dialogBox.setHeadingText("Remote Procedure Call");
		//dialogBox.setAnimationEnabled(true);
		final TextButton closeButton = new TextButton("Close");
		// We can set the id of a widget by accessing its Element
		closeButton.getElement().setId("closeButton");
		final Label textToServerLabel = new Label();
		final HTML serverResponseLabel = new HTML();
		
		// GWT Dependency Injection recipes using MVP4G
		VehiculeView view = new VehiculeView();
		secondLayout.add(view);
		
		// Vertical Layout Container
		VerticalLayoutContainer layout = new VerticalLayoutContainer();
		dialogBox.setWidget(layout);
		
		ContentPanel dialogVPanel = new ContentPanel();
		dialogVPanel.addStyleName("dialogVPanel");
		dialogVPanel.add(new HTML("<b>Sending name to the server:</b>"));
		dialogVPanel.add(textToServerLabel);
		dialogVPanel.add(new HTML("<br><b>Server replies:</b>"));
		dialogVPanel.add(serverResponseLabel);
		dialogVPanel.add(closeButton);
		
		// Add a handler to close the DialogBox
		closeButton.addSelectHandler(new SelectHandler() {
			
			@Override
			public void onSelect(SelectEvent event) {
				// TODO Auto-generated method stub
				dialogBox.hide();
				sendButton.setEnabled(true);
			}
		});

		// Create a handler for the sendButton and nameField
		class MyHandler implements SelectHandler, KeyUpHandler {
			/**
			 * Fired when the user clicks on the sendButton.
			 */
			@Override
			public void onSelect(SelectEvent event) {
				// TODO Auto-generated method stub
				sendNameToServer();
			}

			/**
			 * Fired when the user types in the nameField.
			 */
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					sendNameToServer();
				}
			}

			/**
			 * Send the name from the nameField to the server and wait for a
			 * response.
			 */
			private void sendNameToServer() {
				// First, we validate the input.
				errorLabel.setText("");
				String textToServer = nameField.getText();
				if (!FieldVerifier.isValidName(textToServer)) {
					errorLabel.setText("Please enter at least four characters");
					return;
				}

				// Then, we send the input to the server.
				sendButton.setEnabled(true);
				textToServerLabel.setText(textToServer);
				serverResponseLabel.setText("");
				
				greetingService.greetServer(textToServer,
						new AsyncCallback<String>() {
							public void onFailure(Throwable caught) {
								// Show the RPC error message to the user
								dialogBox.add(new Label("Remote Procedure Call - Failure"));
								serverResponseLabel
										.addStyleName("serverResponseLabelError");
								serverResponseLabel.setHTML(SERVER_ERROR);
								dialogBox.center();
							}

							public void onSuccess(String result) {
								dialogBox.add(new Label("Remote Procedure Call"));
								serverResponseLabel
										.removeStyleName("serverResponseLabelError");
								serverResponseLabel.setHTML(result);
								dialogBox.add(serverResponseLabel);
								dialogBox.center();
								dialogBox.show();
							}
						});
			}

			
		}

		// Add a handler to send the name to the server
		MyHandler handler = new MyHandler();
		sendButton.addSelectHandler(handler);
		nameField.addKeyUpHandler(handler);
	}

}
